//
//  LocationController.m
//  Ghac 4S
//
//  Created by apple on 11-8-23.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import "Global.h"
#import "LocationController.h"
#import "DataFactory.h"
#import "KXML.h"
#import "KXPath.h"
#import "KXMLNode.h"
#import "Shop.h"
#import "SSSSShopController.h"

@interface LocationController(private)
-(void) setUpWithArray:(NSArray*)list;
@end

@implementation LocationController
@synthesize mapView = m_mapView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
-(void)setUpWithData:(NSArray*)list
{
    KRELEASE(m_shopArray);
    m_shopArray = [list retain];
}
-(void)setUpWithArray:(NSArray *)list
{
    [m_mapView addAnnotations:list];
}
#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
     
    m_mapView.userInteractionEnabled = YES;
    m_mapView.showsUserLocation = YES;
    [m_mapView setDelegate:self];
    //ask for data
    [self setUpWithArray:m_shopArray];

}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    CLLocationCoordinate2D coordinate = m_mapView.userLocation.coordinate;
    MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, MKCoordinateSpanMake(3, 3));
    [m_mapView setRegion:region animated:YES]; 
    NSLog(@"update user location");
}
-(void)mapViewWillStartLoadingMap:(MKMapView *)mapView
{
    NSLog(@"start loading map");
}
-(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
    NSLog(@"finish loading map");
}
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    m_currentShop = view.annotation;
    //    currentShop = view.annotation;
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [[SSSSShopController mainInstance] callDetailViewWithShop:view.annotation];

}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if ([annotation isEqual:m_mapView.userLocation])
    {
        return [mapView viewForAnnotation:annotation];
    }
    
    MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];

    pinView.canShowCallout = YES;
    UIButton* button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//    [button addTarget:self action:@selector(annotationSelect:events:) forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = button;
    pinView.animatesDrop = YES;

    return [pinView autorelease];
}
-(void)annotationSelect:(Shop*) target events:(id) event
{
//    NSLog(@"hello, moto, %@", m_currentShop.name);
}
-(void)onData:(id)sender
{
    KXML* xml = [[KXML alloc] initWithData:sender];
    KXPath* path = [[KXPath alloc] initWithString:@"shop" predicate:Nil];
    NSArray* list = [xml arrayByXPath:path];
    for(KXMLNode* node in list)
    {
        Shop* shop = [[Shop alloc] initWithNode:node];
        [m_shopArray addObject:shop];
//        NSLog(@"shop, %@, %f, %f", shop.title, shop.coordinate.latitude, shop.coordinate.longitude);
        [shop release];
    }
    [m_mapView addAnnotations:m_shopArray];
    
    


}
-(void)onError:(id)sender
{
    NSLog(@"error");
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)dealloc
{
    [self viewDidUnload];
    KRELEASE(m_shopArray);
    [super dealloc];
}
@end
